package com.spring.test_back_end.controller;

import com.alibaba.fastjson.JSONObject;
import com.spring.test_back_end.base.dao.UserMapper;
import com.spring.test_back_end.base.dao.pojo.User;
import com.spring.test_back_end.base.daoEx.UserMapperEx;
import com.spring.test_back_end.base.daoEx.pojo.UserEx;
import com.spring.test_back_end.task.AsyncTask;
import com.spring.test_back_end.utils.JwtUtil;
import com.spring.test_back_end.utils.KeyUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.security.Key;
import java.security.SignatureException;
import java.util.*;
import java.util.concurrent.Future;

import static com.spring.test_back_end.bean.Constant.JWT_KEY;

@Controller
@RequestMapping(path = "index")
public class Index {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserMapperEx userMapperEx;

    @Autowired
    private AsyncTask asyncTask;

    @Autowired
    private PropertySourcesPlaceholderConfigurer properties;

    @ResponseBody
    @RequestMapping(path = "hola", method = RequestMethod.GET)
    public Object sayHello() {
        List<User> users = userMapper.selectAll();
        return users;
    }

    @ResponseBody
    @RequestMapping(path = "hola3/{name}", method = RequestMethod.GET)
    public Object sayHello23(@PathVariable String name) {
        System.out.println(name);
        User user = new User();
        user.setId(1);
        user.setName(name);
        Integer i = userMapper.updateByPrimaryKeySelective(user);
        return i;
    }

    @RequestMapping(path = "page")
    public String goAhead(ModelMap modelMap) {
        modelMap.addAttribute("namex", "vijay");
        return "index";
    }

    @ResponseBody
    @RequestMapping(path = "async")
    public String goAhead2() {
        try {
//            Future<String> future = asyncTask.doTaskOne();
//            String res = future.get(); //会进行阻塞一直到拿到返回值为止
//            System.out.println(res);
            asyncTask.doTaskTwo();
            asyncTask.doTaskThree();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "index";

    }

    @ResponseBody
    @RequestMapping(path = "hola2", method = RequestMethod.GET)
    public Object sayHello2(String codes) {
        Map<String, Object> claims = new HashMap<String, Object>();//创建payload的私有声明（根据特定的业务需要添加，如果要拿这个做验证，一般是需要和jwt的接收方提前沟通好验证方式的）
        claims.put("song", "DSSFAWDWADAS...");
        String jwtStr = JwtUtil.createJWT(String.valueOf(Math.random() * 10), JSONObject.toJSONString(claims));
        if (codes.length() > 0) {
            JwtUtil.parseJWT(codes);
        }

        return jwtStr;
    }

}
